﻿using MODEL;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;

namespace DAL
{
    public class GoodsService
    {
        private MMS.DAL.DBHelper db = MMS.DAL.DBHelper.GetDBHelper();
        public GoodsInfo QueryGoodsByCode(string code)
        {
            GoodsInfo g = new GoodsInfo();
            string sql = "select * from GoodsInfo where goodscode like '%" + code + "%'";
            DbDataReader dr = db.GetReader(sql);

            if (dr.Read())
            {

                g.Company = Convert.ToInt32(dr["Company"]);
                g.SalePrice = Convert.ToDouble(dr["SalePrice"]);
                g.GoodsCode = dr["GoodsCode"].ToString();
                g.GoodsName = dr["GoodsName"].ToString();
                g.GoodsState = Convert.ToInt32(dr["GoodsState"]);
                g.OriginPrice = Convert.ToDouble(dr["OriginPrice"]);
                g.Stock = Convert.ToInt32(dr["Stock"]);
                g.Supplier = Convert.ToInt32(dr["Supplier"]);
                g.Unit = dr["Unit"].ToString();
            }
            dr.Close();
            db.Close();
            return g;
        }

        public List<GoodsInfo> QueryListByCode(string code)
        {
            List<GoodsInfo> list = new List<GoodsInfo>();
            string sql = "select * from GoodsInfo where goodscode like '%" + code + "%'";
            DbDataReader dr = db.GetReader(sql);

            while (dr.Read())
            {
                GoodsInfo g = new GoodsInfo();
                if (dr["Company"] != DBNull.Value)
                {
                    g.Company = Convert.ToInt32(dr["Company"]);
                }
                g.SalePrice = Convert.ToDouble(dr["SalePrice"]);
                g.GoodsCode = dr["GoodsCode"].ToString();
                g.GoodsName = dr["GoodsName"].ToString();
                g.GoodsState = Convert.ToInt32(dr["GoodsState"]);
                g.OriginPrice = Convert.ToDouble(dr["OriginPrice"]);

                if (dr["Stock"] != DBNull.Value)
                {
                    g.Stock = Convert.ToInt32(dr["Stock"]);
                }
                if (dr["Supplier"] != DBNull.Value)
                {
                    g.Supplier = Convert.ToInt32(dr["Supplier"]);
                }
                g.Unit = dr["Unit"].ToString();
                list.Add(g);
            }
            dr.Close();
            db.Close();
            return list;
        }
    }
}
